home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
wil4c10.zip
/
WHO_C.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-07-26
|
8KB
|
308 lines
/*
** WHO_C.C [WHOIS Client] ---
*/
#include <windows.h>
#include <winsock.h>
#include "wil.h"
#include "message.h"
#include "paint.h"
#include "about.h"
#include "str.h"
#ifdef WIN32
#define USE_INS HINSTANCE
#define USE_PTR PSTR
#else
#define USE_INS HANDLE
#define USE_PTR LPSTR
#endif
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
/* public globals */
HWND hMainWnd; /* main window handle */
#define BS 8
#define LF 10
#define CR 13
#define MAX_STR 40
#define MAX_BUF 128
#define ONE_SEC 1000
#define TEN_SEC 10000
#define WHOIS_PORT 5001
static HMENU hMenu;
static USE_INS hInstance;
static int WinWidth = 8 * NCOLS;
static int WinHeight = 12 * NROWS + 48;
static char Temp[MAX_BUF+8];
static int InBufLen = 0;
static char InBuffer[MAX_BUF+1];
static char User[MAX_STR] = "\0";
static char Host[MAX_STR] = "\0";
static SOCKET Socket = 0;
static ULONG HostAddr = 0;
static LPSTR Ptr;
/* miscellaneous functions */
static void Add2Buffer(char Chr)
{/* add char to input buffer */
switch(Chr)
{case BS:
if(InBufLen>0)
{/* backup on screen */
DisplayChar(BS);
/* remove last char from buffer */
InBufLen--;
}
break;
default:
/* display char */
DisplayChar(Chr);
/* put into buffer */
if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
break;
}
}
/* display error message */
static void DisplayError(int Code, LPSTR Msg)
{DisplayString("ERROR: ");
if(Msg) DisplayString(Msg);
if(Code)
{wilErrorText(Code,(LPSTR)Temp,50);
DisplayLine((LPSTR)Temp);
}
}
/* WinMain */
#ifdef WIN32
int WINAPI
#else
int PASCAL
#endif
WinMain(USE_INS hInst, USE_INS hPrevInstance,
USE_PTR szCmdLine, int nCmdShow)
{WNDCLASS wc;
MSG msg;
BOOL Result;
if(!hPrevInstance)
{/* register main window class */
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInst, "HostIcon");
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "HostMenu";
wc.lpszClassName = "HostWClass";
Result = RegisterClass(&wc);
if(!Result) return FALSE;
}
/* create main window */
hInstance = hInst;
hMainWnd = CreateWindow(
"HostWClass", "WHOIS Client", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WinWidth, WinHeight,
NULL, NULL,
hInstance, NULL);
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
hMenu = GetMenu(hMainWnd);
/* window control loop */
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
} /* end WinMain */
#ifdef WIN32
LRESULT CALLBACK
#else
long FAR PASCAL
#endif
MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
{int Code;
HDC hDC;
PAINTSTRUCT ps;
#ifdef WIN32
#else
static FARPROC lpfnAboutDlgProc;
#endif
hMainWnd = hWindow;
switch (iMsg)
{case WM_CREATE:
#ifdef WIN32
#else
/* create thunk for Win16 */
lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
#endif
/* initialize paint module */
PaintInit();
/* attach WINSOCK */
DisplayString("Attaching WINSOCK...");
Code = wilAttach();
DisplayLine("OK");
if(Code<0) DisplayError(Code,"wilAttach fails:");
else
{wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
DisplayLine((LPSTR)Temp);
wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
DisplayLine((LPSTR)Temp);
wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
DisplayLine((LPSTR)Temp);
}
break;
case WM_COMMAND:
switch(wParam)
{case MSG_ABOUT :
#ifdef WIN32
DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
#else
DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
#endif
return 0;
case MSG_EXIT:
wilRelease();
DestroyWindow(hMainWnd);
break;
case MSG_DEBUG:
Code = wilDebug(0);
wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
DisplayLine((LPSTR)Temp);
break;
case MSG_WHOIS:
InBufLen = 0;
DisplayString("WHOIS: Enter user@host:");
break;
}
break;
case WM_PAINT:
HideCaret(hMainWnd);
hDC = BeginPaint(hMainWnd, &ps);
SetMapMode(hDC,MM_ANISOTROPIC);
SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
PaintMain(hDC,&ps);
EndPaint(hMainWnd,&ps);
SetCaretPos(PaintGetColPos(),PaintGetRowPos());
ShowCaret(hMainWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CHAR:
if(wParam==CR)
{/* do the CR */
DisplayChar((char)wParam);
/* user has completed input */
DisplayChar(LF);
InBuffer[InBufLen] = '\0';
wsprintf((LPSTR)Temp,"WHOIS %s", InBuffer);
DisplayLine((LPSTR)InBuffer);
/* extract user & host names */
Ptr = StringChar((LPSTR)InBuffer,'@');
if(Ptr==NULL)
{DisplayError(Code, "Cannot recognize User@Domain");
break;
}
*Ptr = '\0';
lstrcpy((LPSTR)User, (LPSTR)InBuffer);
lstrcpy((LPSTR)Host, (LPSTR)(++Ptr));
wsprintf((LPSTR)Temp,"User ='%s'", (LPSTR)User);
DisplayLine(Temp);
wsprintf((LPSTR)Temp,"Domain ='%s'", (LPSTR)Host);
DisplayLine(Temp);
/* ask for host by name */
Code = wilAskHostByName((LPSTR)Host);
if(Code<=0)
{DisplayError(Code, NULL);
break;
}
/* get host IP address */
HostAddr = wilGetHostAddr(0);
if(HostAddr==0)
{DisplayError(0, "Cannot get IP addess");
break;
}
wsprintf((LPSTR)Temp,"HostAddr = %s\n", wilGetHostDotted(0) );
DisplayLine((LPSTR)Temp);
/* create TCP socket */
Socket = wilTcpSocket();
if((int)Socket<0)
{DisplayError((int)Code, NULL);
break;
}
/* attempt to connect to remote host */
Code = wilConnect(Socket,HostAddr,WHOIS_PORT);
if(Code<=0)
{DisplayError(Code, NULL);
break;
}
/* wait up to 10 seconds for a response */
if(wilIsConnected(Socket,10000))
{wsprintf((LPSTR)Temp,"Connected: Addr=%lx Port=%d",
wilRemoteSockAddr(Socket), wilRemoteSockPort(Socket) );
DisplayLine((LPSTR)Temp);
}
else
{DisplayLine("Cannot CONNECT");
break;
}
/* send user name */
wsprintf((LPSTR)Temp,"Whois [%s]",(LPSTR)User);
DisplayLine((LPSTR)Temp);
Code = wilWriteString(Socket,(LPSTR)User);
if(Code<=0)
{DisplayError(Code, NULL);
break;
}
/* read response */
while(1)
{if(!wilDataIsReady(Socket,TEN_SEC)) break;
Code = wilReadString(Socket,(LPSTR)InBuffer,MAX_BUF);
if(Code==WIL_EOF) break;
if(Code>0)
{wsprintf((LPSTR)Temp,"%s", (LPSTR)InBuffer);
DisplayString((LPSTR)Temp);
}
DisplayLine("<DONE>");
wilCloseSocket(Socket);
}
}
else
{/* add char to input buffer */
Add2Buffer((char) wParam);
}
break;
default:
return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
}
return 0;
} /* end MainWndProc */